<?php

/**
 * 后台操作日志控制器
 * @author yupoxiong<i@yupoxiong.com>
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\model\PageClickLog;
use Exception;
use JsonException;
use think\Request;
use think\response\Json;
use app\admin\model\PageLog;
use app\common\model\Country;
use app\common\model\Page;

class PageLogController extends AdminBaseController
{
    protected array $authExcept = [
        'admin/page_log/position'
    ];
    /**
     * 列表
     * @param Request $request
     * @param AdminLog $model
     * @return string
     * @throws Exception
     */
    public function index(Request $request, PageLog $model): string
    {
        $param = $request->param();
        if (!isset($param['start_time'])) {
            $param['start_time'] = date('Y-m-d') . ' 00:00:00';
            $param['end_time'] = date('Y-m-d') . ' 23:59:59';
        } else {
            $param['start_time'] = $param['start_time'] . ' 00:00:00';
            $param['end_time'] = $param['end_time'] . ' 23:59:59';
        }
        $model = $model->whereBetweenTime('create_time', $param['start_time'], $param['end_time']);
        $redata = $model->select();
        $data  = $model->distinct(true)->field('page_keys')
            ->paginate([
                'list_rows' => $this->admin['admin_list_rows'],
                'var_page'  => 'page',
                'query'     => $request->get()
            ]);
        $pageclicklogModel = new PageClickLog();
        $country_key_val = (new Country)->getkeyval();
        foreach ($data as $k => $v) {
            $uv = [];
            $ipuv=[];
            $pv = 0;
            foreach ($redata as $rev) {
                if ($v['page_keys'] == $rev['page_keys']) {
                    $pv++;
                    $uv[$rev['usercode']] = 1;
                    $ipuv[$rev['ip']] = 1;
                }
            }
            $data[$k]['pv'] = $pv;
            $data[$k]['uv'] = count($uv);
            $data[$k]['ipuv'] = count($ipuv);
            $data[$k]['country'] = $country_key_val[Page::where('keys', $v['page_keys'])->value('country')];
            $data[$k]['butjson'] =  $pageclicklogModel->getClick($v['page_keys'], $param);
        }
        // 关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            // 'admin_user_list'=>(new AdminUser())->select(),
        ]);
        return $this->fetch();
    }

    /**
     * @param $id
     * @param AdminLog $model
     * @return string
     * @throws Exception
     */
    public function detail($id, PageLog $model, Request $request): string
    {
        $data = $model->where('page_keys', $id)->order('id desc')->paginate([
            'list_rows' => $this->admin['admin_list_rows'],
            'var_page'  => 'page',
            'query'     => $request->get(),
        ]);

        $this->assign([
            'data' => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);

        return $this->fetch();
    }

    public function click_detail($id, PageClickLog $model): string
    {
        $data = $model->where('page_log_id', $id)->order('id desc')->select();

        $this->assign([
            'data' => $data,
        ]);

        return $this->fetch();
    }
}
